//
//  EpointDWS_CheckLoginTask.m
//  EpointFrame5
//
//  Created by 李亚春 on 13-12-17.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "EpointDWS_CheckLoginTask.h"
#import "EpointWebServiceUtil.h"
#import "MDUtil.h"
#import "EpointStringUtil.h"
#import "GTMBase64.h"
#import "EncryptUtil.h"

@implementation EpointDWS_CheckLoginTask

-(void)execute
{
    NSString *loginid = [params objectForKey:@"loginid"];
    NSString *psw = [params objectForKey:@"psw"];
    
    NSString *deviceFlag = @"3";
    
    //判断是iPad还是iPhone
    if (CurrentDeviceISiPad)
    {
        deviceFlag = @"4";
    }
    
    //判断是否启用了中间平台
    NSString *MobileManagerEnable = NSLocalizedString(@"MobileManagerEnable", @"");

    if ([MobileManagerEnable isEqualToString:@"1"]) {
        
        //中间平台验证
        NSString *Epoint_MobileManager_URL = NSLocalizedString(@"Epoint_MobileManager_URL", @"");
        NSString *managerNS = @"http://server.service.axis2";
        NSString *idfv =[[[UIDevice currentDevice] identifierForVendor]UUIDString];
        EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:Epoint_MobileManager_URL namespace:managerNS method:@"checkLoginInfo"];
        [ws addParameterForString:@"IMEI" value:idfv];
        [ws addParameterForString:@"LoginId" value:loginid];
        [ws addParameterForString:@"MobileVersion" value:deviceFlag];
        [ws addParameterForString:@"DeviceId" value:idfv];
        [ws addParameterForString:@"InterfaceFlag" value:NSLocalizedString(@"InterfaceFlag", @"1")];
        NSString *bs = ws.start;
        NSString *status = [EpointStringUtil getXMLFormatedAttribute:bs :@"Status"];
        if ([status isEqualToString:@"True"])
        {
            NSString *UserArea = [EpointStringUtil getXMLFormatedAttribute:bs :@"UserArea"];
            NSString *BusinessWebServiceUrl = [EpointStringUtil getXMLFormatedAttribute:UserArea :@"BusinessWebServiceUrl"];
            NSString *needHandwrite = [EpointStringUtil getXMLFormatedAttribute:UserArea :@"needHandwrite"];
            NSString *iOSPushType = [EpointStringUtil getXMLFormatedAttribute:UserArea :@"iOSPushType"];
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:BusinessWebServiceUrl forKey:Epoint_MobileOAWebService_URL];
            [userDefaults setObject:needHandwrite forKey:Epoint_NeedHandWrite_Flag];
             [userDefaults setObject:iOSPushType forKey:Epoint_iOSPushType];
  
            NSString *mobileConfig = [Epoint_MobileManager_URL stringByReplacingOccurrencesOfString:@"services/MobileOaManage?wsdl" withString:@"mobileconfig/ios.xml"];
            
            [userDefaults setObject:mobileConfig forKey:Epoint_User_MainConfig_URL];
            [userDefaults synchronize];
            [self checkMobileOALoginId:loginid andPsw:psw];
        }
        else
        {
            NSString *Msg = [EpointStringUtil getXMLFormatedAttribute:bs :@"Description"];
            if ([Msg isEqualToString:@""]) {
                Msg = @"网络连接超时！";
            }
            [self exeFailureWithMessage:Msg];
        }
    }
    else
    {
        [self checkMobileOALoginId:loginid andPsw:psw];
    }
}

//移动OA登陆验证
-(void)checkMobileOALoginId:(NSString*) loginid andPsw:(NSString *)psw
{
    NSString *url = [self getMobileOAWebserviceURL];
    NSString *validata = [EpointBizTools getToken];
    NSString *namespace = NSLocalizedString(@"Epoint_DNet_WS_NameSpace", @"");
    loginid = [EpointStringUtil filterXmlString:loginid];
    EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:namespace method:@"UserLogin2"];
    
    psw = [EpointStringUtil filterXmlString:[MDUtil authPassword:psw]];
    loginid = [EpointStringUtil filterXmlString:loginid];
    
    
    NSString* CertBase64 = @"";
    NSString* path = [[NSBundle mainBundle] pathForResource:@"z15"
                                                     ofType:@"cer"];
    NSData* data = [[NSData alloc] initWithContentsOfFile:path];
    CertBase64 =[[NSString alloc] initWithData:[GTMBase64 encodeData:data] encoding:NSUTF8StringEncoding];
    
   
    NSString *ParasXml = [NSString stringWithFormat:@"<![CDATA[<?xml version=\"1.0\" encoding=\"gb2312\"?><paras><LoginID>%@</LoginID><Password>%@</Password></paras>]]>",loginid,psw];
    [ws addParameterForString:@"ValidateData" value:validata];
    [ws addParameterForString:@"ParasXml" value:ParasXml];
//    NSString *bsDnet = [ws starts:CertBase64];
    NSString *bsDnet = ws.start;
    if ([self validateXMLWS:bsDnet]) {
        NSString *UserArea =[EpointStringUtil getXMLFormatedAttribute:bsDnet :@"UserArea"];
        NSString *UserLogin =[EpointStringUtil getXMLFormatedAttribute:UserArea :@"UserLogin"];
        if ([UserLogin isEqualToString:@"True"])
        {
            NSString *UserGuid = [EpointStringUtil getXMLFormatedAttribute:UserArea :@"UserGuid"];
            NSString *UserDisplayName = [EpointStringUtil getXMLFormatedAttribute:UserArea :@"UserDisplayName"];
            NSString *BaseOuGuid = [EpointStringUtil getXMLFormatedAttribute:UserArea :@"BaseOuGuid"];
            NSString* OAVesion =[EpointStringUtil getXMLFormatedAttribute:UserArea :@"OAVesion"];
            if (OAVesion==nil||[OAVesion isEqualToString:@""]) {
                OAVesion=@"8.0";
            }
            
            [DBFrameService setSecurityKey:Epoint_User_LoginId Value:loginid];
            [DBFrameService setSecurityKey:Epoint_User_Psw Value:[params objectForKey:@"psw"]];
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            
            [userDefaults setObject:UserGuid forKey:Epoint_User_UserGuid];
            [userDefaults setObject:UserDisplayName forKey:Epoint_User_DisplayName];
            [userDefaults setObject:url forKey:Epoint_MobileOAWebService_URL];
            [userDefaults setObject:BaseOuGuid forKey:Epoint_User_BaseOuGuid];
            [userDefaults setObject:OAVesion forKey:Epoint_OAVesion];
            
            //保存主界面配置URL
            NSString *Epoint_MobileManager_URL = NSLocalizedString(@"Epoint_MobileManager_URL", @"");
            NSString *mobileConfig = [Epoint_MobileManager_URL stringByReplacingOccurrencesOfString:@"services/MobileOaManage?wsdl" withString:@"mobileconfig/ios.xml"];
            
            [userDefaults setObject:mobileConfig forKey:Epoint_User_MainConfig_URL];
            [userDefaults synchronize];
            [self exeSuccess];
        }
        else if([UserLogin isEqualToString:@"False"])
        {
            NSString *msg = [EpointStringUtil getXMLFormatedAttribute:UserArea :@"Description"];
            [self exeFailureWithMessage:msg];
        }
        else
        {
            [self exeFailureWithMessage:@"未知错误"];
        }
    }
}



@end
